package com.ruoyi.service.impl;

import com.ruoyi.dto.AppDTO;
import com.ruoyi.mapper.AppMapper;
import com.ruoyi.service.AppService;
import com.ruoyi.vo.AppZoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @Author:zzm
 * @Date: 2024/12/10 15:25
 */
@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppMapper appMapper;
    @Override
    public AppZoneVO selectCost(AppDTO dto) {
        AppZoneVO vo = new AppZoneVO();
        vo = AppZoneVO.fillNull(vo);
        //英尺换算--0.393700787402
        BigDecimal acre = new BigDecimal("0.393700787402");
        BigDecimal conversionFactor = new BigDecimal("2.2046");
        //实重(LB)
        BigDecimal actualWeight = dto.getWeight().multiply(conversionFactor).setScale(2, RoundingMode.HALF_UP);
        //长宽高转为英尺
        BigDecimal length = dto.getLength();
        BigDecimal acreLength = length.multiply(acre).setScale(0, RoundingMode.UP);
        BigDecimal width =dto.getWidth();
        BigDecimal acreWidth = width.multiply(acre).setScale(0, RoundingMode.UP);
        BigDecimal height = dto.getHeight();
        BigDecimal acreHeight = height.multiply(acre).setScale(0, RoundingMode.UP);
        //体积
        BigDecimal volume = acreLength.multiply(acreWidth).multiply(acreHeight).setScale(2, RoundingMode.HALF_UP);
        //围长
        // 获取最大值
        BigDecimal maxVal = acreLength.max(acreWidth).max(acreHeight);
        //总长度
        BigDecimal all = acreLength.add(acreWidth).add(acreHeight);
        // 计算另外两个值的和
        BigDecimal sumOfOtherTwo = all.subtract(maxVal);
        // 计算最短边
        BigDecimal minSide = acreLength.min(acreWidth).min(acreHeight);
        // 计算第二长的边
        BigDecimal secondMaxSide = all.subtract(maxVal).subtract(minSide);
        // 最终结果：最大值 + 2 * (另外两个值相加)
        BigDecimal wc = maxVal.add(sumOfOtherTwo.multiply(new BigDecimal("2")));
        //体积重
        BigDecimal last = volume.divide(new BigDecimal("250"), 2, RoundingMode.HALF_UP);
        BigDecimal billingWeight = actualWeight.compareTo(last) > 0 ? actualWeight : last;

        //AHS根据实重判断
        //超尺寸标识
        Boolean isAHSDimensions = false;
        //超重标识
        Boolean isAHSWeight = false;
        //OverSize
        Boolean isOverSize = false;
        if((maxVal.compareTo(new BigDecimal("96")) >= 0 && maxVal.compareTo(new BigDecimal("108")) <=0) ||
                (wc.compareTo(new BigDecimal("130")) > 0)){
            isOverSize = true;
        }
        //超尺寸 48inches≤包裹最长边≤96inches/第二长的边长超过30inches/长+2*（宽+高）≥105inches
        if((secondMaxSide.compareTo(new BigDecimal("30")) >0) ||
                (maxVal.compareTo(new BigDecimal("48")) >= 0 && maxVal.compareTo(new BigDecimal("96")) <=0) ||
                    (wc.compareTo(new BigDecimal("105")) > 0 && wc.compareTo(new BigDecimal("129")) <= 0) ){
            isAHSDimensions=true;
        }
        if(actualWeight.compareTo(new BigDecimal(50.00))>=0 ){
            isAHSWeight=true;
        }

        List<Double> foundation;
        if(isAHSDimensions ||isAHSWeight ||isOverSize ){
            //如果是ahs查type为1的基础费用
            foundation = appMapper.getFoundationByType(billingWeight.setScale(0, RoundingMode.CEILING),"1");
            //住宅附加费
            vo.setAddress(new double[]{2.90, 2.90, 2.90,2.90, 2.90,2.90, 2.90});
        }else{
            foundation = appMapper.getFoundationByType(billingWeight.setScale(0, RoundingMode.CEILING),"0");
            //住宅附加费
            vo.setAddress(new double[]{0, 0, 0,0, 0,0, 0});
        }
        //基础运费
        vo.setFoundation(foundation.stream().mapToDouble(Double::doubleValue).toArray());
        //AHS/Oversize
        if(isOverSize){
            vo.setAhs(new double[]{24.00, 26.00, 26.00, 35.00, 35.00, 37.00, 37.00});
        }else if(isAHSWeight){
            vo.setAhs(new double[]{5.70, 5.90, 5.90, 6.30, 6.30, 6.90, 6.90});
        }else if(isAHSDimensions){
            vo.setAhs(new double[]{3.50, 3.90, 3.90,4.40, 4.40,4.90, 4.90});

        }

        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!基础费用="+vo.getFoundation().length);
        if(vo.getFoundation().length!=0){
            for (int i = 0; i <7; i++) {
                double value= (vo.getFoundation()[i] + vo.getAhs()[i]+vo.getAddress()[i])*0.16;
                BigDecimal rounded = BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP);
                vo.getFuel()[i] = rounded.doubleValue();
            }
            //总费用
            for (int i = 0; i <7; i++) {
                double value= vo.getFoundation()[i] + vo.getAhs()[i]+vo.getAddress()[i]+vo.getFuel()[i];
                BigDecimal rounded = BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP);
                vo.getMoney()[i] = rounded.doubleValue();
            }
            return vo;
        }else{
            return null;
        }
    }


}













